package org.study.bookmanager.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateUtils {

    // 默认日期格式
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    private static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    // Date -> LocalDate
    public static LocalDate dateToLocalDate(Date date) {
        return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();
    }

    // Date -> LocalDateTime
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }

    // LocalDate -> Date
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate
                .atStartOfDay(ZoneId.systemDefault())
                .toInstant());
    }

    // LocalDateTime -> Date
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime
                .atZone(ZoneId.systemDefault())
                .toInstant());
    }

    // 格式化 LocalDate
    public static String formatLocalDate(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
    }

    // 格式化 LocalDateTime
    public static String formatLocalDateTime(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));
    }

    // 解析字符串为 LocalDate
    public static LocalDate parseToLocalDate(String dateString) {
        return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
    }

    // 解析字符串为 LocalDateTime
    public static LocalDateTime parseToLocalDateTime(String dateTimeString) {
        return LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));
    }

    // 计算两个日期间的天数差
    public static long daysBetween(LocalDate startDate, LocalDate endDate) {
        return ChronoUnit.DAYS.between(startDate, endDate);
    }

    // 增加天数到 LocalDate
    public static LocalDate addDays(LocalDate localDate, int days) {
        return localDate.plusDays(days);
    }

    // 增加天数到 LocalDateTime
    public static LocalDateTime addDays(LocalDateTime localDateTime, int days) {
        return localDateTime.plusDays(days);
    }

    public static Date addDays(Date date,int days){
        return localDateToDate(dateToLocalDate(date).plusDays(days));
    }
    // 获取当前日期
    public static LocalDate getCurrentDate() {
        return LocalDate.now();
    }

    // 获取当前日期和时间
    public static LocalDateTime getCurrentDateTime() {
        return LocalDateTime.now();
    }

    // 获取当天的开始时间
    public static LocalDateTime getStartOfDay(LocalDate localDate) {
        return localDate.atStartOfDay();
    }

    // 获取当天的结束时间
    public static LocalDateTime getEndOfDay(LocalDate localDate) {
        return localDate.atTime(LocalTime.MAX);
    }
//    public static void main(String[] args) {
//        // 示例用法
//        LocalDate today = getCurrentDate();
//        System.out.println("今天的日期: " + formatLocalDate(today));
//
//        LocalDateTime now = getCurrentDateTime();
//        System.out.println("现在的日期和时间: " + formatLocalDateTime(now));
//
//        LocalDate parsedDate = parseToLocalDate("2023-06-05");
//        System.out.println("解析后的日期: " + formatLocalDate(parsedDate));
//
//        LocalDate addedDays = addDays(today, 5);
//        System.out.println("增加5天后的日期: " + formatLocalDate(addedDays));
//
//        long daysBetween = daysBetween(today, addedDays);
//        System.out.println("两日期间的天数差: " + daysBetween);
//    }
}
